<template>
  <div class="view_page empowerlist">
    <el-card class="box-card">
      <!--卡片头部-->
      <div slot="header" class="clearfix">
        <!--左侧-->
        <div class="left">
          <div class="menu-title">
            监测分析
          </div>
        </div>
        <div class="right">
          <el-button type="primary" size="small" @click="navtoAdd('add')"
            >新增
          </el-button>
        </div>
      </div>
      <!--筛选-->
      <div class="query-operate">
        <el-form
          class="demo-form-inline indoor-scene-buzhou-list-form"
          :inline="true"
          :model="TESTFROMDATA"
          @submit.native.prevent
        >
          <el-form-item
            label="名称"
            label-width="120px"
            style="margin-top: 5px; margin-bottom: 5px;"
          >
            <el-input
              clearable
              @clear="searchList()"
              v-model="TESTFROMDATA.name"
              placeholder="请输入名称"
              @keyup.native.enter="searchList()"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="监测状态"
            label-width="80px"
            style="margin-top: 5px; margin-bottom: 5px;"
          >
            <el-select
              v-model="TESTFROMDATA.status"
              clearable
              @change="searchList()"
            >
              <el-option
                v-for="item in stateList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="站点类型"
            label-width="80px"
            style="margin-top: 5px; margin-bottom: 5px;"
          >
            <el-select
              v-model="TESTFROMDATA.river_type"
              clearable
              @change="searchList()"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            style="margin-top: 5px; margin-bottom: 5px;margin-left: 10px;"
          >
            <el-button type="primary" @click="searchList()">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!--卡片内容-->
      <!--列表-->
      <el-table :data="datalist" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center">
        </el-table-column>
        <el-table-column
          prop="status"
          label="监测状态"
          align="center"
          min-width="160"
        >
          <template slot-scope="scope">
            <div>
              {{ statusType(scope.row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="监测名称"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="has_default"
          label="是否默认"
          align="center"
          min-width="160"
        >
          <template slot-scope="scope">
            <div>
              {{ has_defaultType(scope.row.has_default) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="river_type"
          label="站点类型"
          align="center"
          min-width="160"
        >
          <template slot-scope="scope">
            <div>
              {{ riverType(scope.row.river_type) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="site_name"
          label="设备"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="value_name"
          label="值"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="max_num"
          label="最大值"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="min_num"
          label="最小值"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="230" align="center">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;flex-wrap: wrap; ">
              <el-button
                type="primary"
                size="small"
                @click="navtoAdd('update', scope.row.id)"
                >修改
              </el-button>
              <el-button type="danger" size="small" @click="del(scope.row)"
                >删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        :total="total"
        :currentPage="pageNum"
        :pageSize="size"
        position="right"
        @search="searchPagination"
      >
      </pagination>
    </el-card>

    <!--新增修改弹框-->
    <el-dialog
      :close-on-click-modal="false"
      :title="addtype == 'add' ? '新增监控分析配置' : '修改监控分析配置'"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <el-form ref="form" label-width="80px">
        <el-form-item label="监测状态">
          <el-select
            v-model="liveData.status"
            clearable
            filterable
            @change="changeStatus"
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in stateListTC"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监测名称">
          <el-input
            v-model.trim="liveData.name"
            placeholder="请输入名称"
            clearable
            type="text"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否默认">
          <el-switch
            v-model="liveData.has_default"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="站点类型">
          <el-select
            v-model="liveData.river_type"
            clearable
            filterable
            @change="changeRiverType"
            placeholder="请选择站点类型"
          >
            <el-option
              v-for="item in river_typeData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备" v-if="is_type">
          <el-select
            v-model="liveData.site"
            clearable
            filterable
            @change="changeSite"
            placeholder="请选择设备"
          >
            <el-option
              v-for="item in river_idData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="值" v-if="is_value">
          <el-select
            v-model="liveData.value"
            clearable
            filterable
            @change="changeValue"
            placeholder="请选择类型值(单位)"
          >
            <el-option
              v-for="item in valueList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最大值" v-if="is_minimax">
          <el-input
            v-model.trim="liveData.max_num"
            placeholder="请输入最大值"
            clearable
            :change="JYmax_num()"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="最小值" v-if="is_minimax">
          <el-input
            v-model.trim="liveData.min_num"
            placeholder="请输入最小值"
            clearable
            :change="JYmin_num()"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from "element-ui";
import pagination from "@/components/common/pagination/pagination";

export default {
  components: {
    pagination
  },
  data() {
    return {
      token: "",
      userInfo: JSON.parse(localStorage.getItem("loginPersonnelInformation")),
      // 模糊查询数据
      TESTFROMDATA: {
        name: "",
        status: "",
        river_type: ""
      },
      //富文本文档类型
      liveData: {
        name: "",
        type: "",
        value: "",
        river_type: "",
        site: "",
        status: "",
        max_num: "",
        min_num: "",
        has_default: false
      },

      // 选择状态
      stateList: [
        {
          id: -1,
          name: "全部"
        },
        {
          id: 1,
          name: "实时状态"
        },
        {
          id: 2,
          name: "实时监测"
        },
        {
          id: 3,
          name: "安全监测"
        }
      ],
      stateListTC: [
        {
          id: 1,
          name: "实时状态"
        },
        {
          id: 2,
          name: "实时监测"
        },
        {
          id: 3,
          name: "安全监测"
        }
      ],
      typeList: [
        {
          id: -1,
          name: "全部"
        },
        {
          id: 1,
          name: "河流"
        },
        {
          id: 2,
          name: "水库"
        },
        {
          id: 3,
          name: "坑塘"
        }
      ],
      //站点类型
      river_typeData: [
        {
          id: 1,
          name: "河流"
        },
        {
          id: 2,
          name: "水库"
        },
        {
          id: 3,
          name: "坑塘"
        }
      ],
      is_type: false,
      is_value: false,
      is_minimax: false,
      river_idData: [], //河流/水库id

      // 值
      valueList: [],

      // 列表的数据
      datalist: [],
      pageNum: 1,
      page: 1,
      size: 10,
      total: 0,

      // 弹窗
      dialogVisible: false,
      addtype: "", //类型 新增-修改
      itemID: "" //记录选中的id
    };
  },
  created() {
    var _this = this;
    _this.getListPage();
  },
  mounted() {},
  methods: {
    statusType(item) {
      switch (item) {
        case 1:
          return "实时状态";
          break;
        case 2:
          return "实时监测";
          break;
        case 3:
          return "安全监测";
          break;
        default:
          break;
      }
    },
    riverType(item) {
      switch (item) {
        case 1:
          return "河流";
          break;
        case 2:
          return "水库";
          break;
        case 3:
          return "坑塘";
          break;
        default:
          break;
      }
    },
    has_defaultType(item) {
      switch (item) {
        case false:
          return "不默认";
          break;
        case true:
          return "默认";
          break;
        default:
          break;
      }
    },
    initData() {
      this.liveData = {
        name: "",
        type: "",
        value: "",
        river_type: "",
        site: "",
        status: "",
        max_num: "",
        min_num: "",
        has_default: false
      };
    },

    //查询列表
    getListPage() {
      var _this = this;
      var datas = {
        name: _this.TESTFROMDATA.name,
        status: _this.TESTFROMDATA.status,
        river_type: _this.TESTFROMDATA.river_type,
        page: _this.page,
        size: _this.size
      };
      _this.$apis.config_list(datas).then(res => {
        if (res.result == 0) {
          _this.datalist = res.data;
          _this.total = res.rows;
          _this.pageNum = res.page;
        }
      });
    },
    //模糊查询
    searchList() {
      this.page = 1;
      this.datalist = [];
      this.getListPage();
    },
    //分页
    searchPagination(page) {
      this.page = page;
      this.getListPage();
    },
    // TODO监测状态选中
    changeStatus(val) {
      var _this = this;
      // console.log("监测状态val:", val, _this.liveData);
      // // 下下项置空
      // _this.liveData.site = null;
      // _this.river_idData = [];
      // // 下下下项置空
      // _this.is_value = false;
      // _this.liveData.value = null;
      // _this.valueList = [];

      // _this.is_type = false;
      // // 站点列表
      // if (_this.liveData.river_type != "" && _this.liveData.status != "") {
      //   var datas = {
      //     // type: val,
      //     river_type: _this.liveData.river_type
      //   };
      //   _this.$apis.config_equipment_list_all(datas).then(res => {
      //     if (res.result == 0) {
      //       _this.river_idData = res.data;
      //       _this.is_type = true;
      //     }
      //   });
      // }
    },
    // TODO站点类型选中
    changeRiverType(val) {
      var _this = this;
      // console.log("站点类型val:", val, _this.liveData);
      // 设备置空
      _this.liveData.site = null;
      _this.river_idData = [];
      // 值置空
      _this.is_value = false;
      _this.liveData.value = null;
      _this.valueList = [];
      // 最大最小置空
      _this.is_minimax = false;

      _this.is_type = false;
      // 站点列表
      // if (_this.liveData.status != "" && _this.liveData.river_type != "") {
      if (_this.liveData.river_type != "") {
        var datas = {
          // type: _this.liveData.status,
          river_type: val
        };
        _this.$apis.config_equipment_list_all(datas).then(res => {
          if (res.result == 0) {
            _this.river_idData = res.data;
            _this.is_type = true;
          }
        });
      }
    },
    // TODO设备选中
    changeSite(val) {
      var _this = this;
      // console.log("站点:", val, _this.liveData);
      if (val == "") {
        _this.is_value = false;
        _this.liveData.value = null;
        _this.valueList = [];
        _this.is_minimax = false;
        _this.liveData.max_num = "";
        _this.liveData.min_num = "";
      } else {
        _this.is_value = true;
        _this.liveData.value = null;
        _this.valueList = [];

        //取出返回列表里的id的那条数据所有的信息
        var obj = _this.river_idData.find(function(obj) {
          return obj.id == val;
        });
        var return_value = obj.type;

        // 数值详情列表
        var datas = {
          type: return_value
        };
        _this.$apis.config_value_query(datas).then(res => {
          if (res.result == 0) {
            _this.valueList = res.data;
          }
        });
      }
    },
    // TODO 值选中
    changeValue(val) {
      var _this = this;
      // console.log("值:", val, _this.liveData);
      if (val == "") {
        _this.is_minimax = false;
        _this.liveData.max_num = "";
        _this.liveData.min_num = "";
      } else {
        _this.is_minimax = true;
      }
    },

    // 打开弹窗
    navtoAdd(type, item) {
      var _this = this;
      // console.log("打开弹窗type:", type, "item:", item);
      if (type == "add") {
        _this.addtype = "add";
        _this.initData();
        _this.dialogVisible = true;
        _this.is_type = false;
        _this.is_value = false;
        _this.is_minimax = false;
      } else {
        _this.itemID = item;
        _this.addtype = "updata";
        _this.is_type = true;
        _this.is_value = true;
        _this.is_minimax = true;
        var datas = {
          id: _this.itemID
        };
        // TODO查询详情
        _this.$apis.config_query(datas).then(res => {
          if (res.result == 0) {
            _this.liveData = res.data;
            // TODO站点列表处理回显
            var datas = {
              // type: _this.liveData.status,
              river_type: _this.liveData.river_type
            };
            _this.$apis.config_equipment_list_all(datas).then(res => {
              if (res.result == 0) {
                _this.river_idData = res.data;
                _this.is_type = true;

                // TODO数值详情列表回显
                // 取出返回列表里的id的那条数据所有的信息
                var obj = _this.river_idData.find(function(obj) {
                  return obj.id == _this.liveData.site;
                });
                var return_value = obj.type;
                var datas = {
                  type: return_value
                };
                _this.$apis.config_value_query(datas).then(res => {
                  if (res.result == 0) {
                    _this.valueList = res.data;
                  }
                });
              }
            });

            _this.dialogVisible = true;
          }
        });
      }
    },
    //取消
    cancel() {
      this.dialogVisible = false;
    },
    save() {
      var _this = this;
      var datas = JSON.parse(JSON.stringify(_this.liveData));
      console.log("save-datas:", datas);

      if (datas.status == "" || datas.status == null) {
        Message.error("请选择监测状态");
        return;
      }
      if (datas.name == "") {
        Message.error("请输入监测名称");
        return;
      }
      // if (datas.has_default == null) {
      //   Message.error("请选择是否默认");
      //   return;
      // }
      if (datas.river_type == "" || datas.river_type == null) {
        Message.error("请选择站点类型");
        return;
      }
      if (datas.site == "" || datas.site == null) {
        Message.error("请选择设备");
        return;
      }
      if (datas.value == "" || datas.value == null) {
        Message.error("请选择值");
        return;
      }
      if (datas.max_num == "") {
        Message.error("请输入最大值");
        return;
      }
      if (datas.min_num == "") {
        Message.error("请输入最小值");
        return;
      }
      if (Number(datas.min_num) > Number(datas.max_num)) {
        Message.error("最小值不能大于最大值");
        return;
      }
      if (_this.addtype == "add") {
        _this.$apis.config_add(datas).then(res => {
          if (res.result == 0) {
            Message.success("添加成功");
            this.dialogVisible = false;
            this.getListPage();
          }
        });
      } else {
        datas.id = _this.itemID;
        _this.$apis.config_update(datas).then(res => {
          if (res.result == 0) {
            Message.success("修改成功");
            this.dialogVisible = false;
            this.getListPage();
          }
          // else {
          //   this.dialogVisible = false;
          //   this.getListPage();
          // }
        });
      }
    },
    // 删除
    del(item) {
      var _this = this;
      var datas = {
        id: item.id
      };
      _this
        .$confirm("确定要删除ID为【" + item.id + "】的数据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          _this.$apis.config_del(datas).then(res => {
            if (res.result == 0) {
              Message.success("删除成功");
              if (_this.page > 1 && _this.datalist.length <= 1) {
                _this.page = _this.page - 1;
              }
            }
            //查询列表
            _this.getListPage();
          });
        })
        .catch(() => {});
    },
    JYmax_num() {
      var _this = this;
      let checkPlan = "" + _this.liveData.max_num;
      let first = checkPlan.charAt(0);
      checkPlan = checkPlan
        .replace(/[^\d.]/g, "")
        .replace(/\.{2,}/g, ".")
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".")
        .replace(/^(\-)*(\d+)\.(\d{1,10}).*$/, "$1$2.$3")
        .replace(/^\./g, "");
      if (checkPlan.indexOf(".") < 0 && checkPlan !== "") {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        checkPlan = parseFloat(checkPlan) + "";
      } else if (checkPlan.indexOf(".") >= 0) {
        checkPlan = checkPlan.replace(/^()*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数
      }
      if (checkPlan && first == "-") {
        checkPlan = "-" + checkPlan;
      }
      _this.liveData.max_num = checkPlan;
    },
    JYmin_num() {
      var _this = this;
      let checkPlan = "" + _this.liveData.min_num;
      let first = checkPlan.charAt(0);
      checkPlan = checkPlan
        .replace(/[^\d.]/g, "")
        .replace(/\.{2,}/g, ".")
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".")
        .replace(/^(\-)*(\d+)\.(\d{1,10}).*$/, "$1$2.$3")
        .replace(/^\./g, "");
      if (checkPlan.indexOf(".") < 0 && checkPlan !== "") {
        // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
        checkPlan = parseFloat(checkPlan) + "";
      } else if (checkPlan.indexOf(".") >= 0) {
        checkPlan = checkPlan.replace(/^()*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数
      }
      if (checkPlan && first == "-") {
        checkPlan = "-" + checkPlan;
      }
      _this.liveData.min_num = checkPlan;
    }
  }
};
</script>

<style>
.empowerlist .el-dialog__header {
  border-bottom: 1px solid #e3e3e3;
}
.column-bz {
  white-space: nowrap !important;
  overflow: hidden !important;
  text-overflow: ellipsis !important;
}
.empower-time {
  width: 100% !important;
}
/**站内信长单词强制换行 */
.message_box_alert {
  word-break: break-all !important;
}
.key-content .el-form-item__content {
  margin-left: 0 !important;
}
</style>
